package 代码记录.校招笔试真题.笔试真题_2023.博乐科技;

import java.util.Arrays;

/**
 * @author zx
 * @create 2023-02-19 20:47
 */
public class Test2 {
    /**
     * 希尔排序衍生 100%
     */
    private static int res = 0;
    public int shell_sort (int[] numlist, int[] gaplist) {
        for(int i = 0;i < gaplist.length;i++){
            if(!isOk(numlist) && gaplist[i] > 0){
                shell_sortByGap(numlist,gaplist[i]);
            }else if(isOk(numlist)){
                return res;
            }
        }
        return res;
    }
    private void shell_sortByGap(int[] arr, int gap){
        for(int i = gap;i < arr.length;i++){
            for(int j = i;j >= gap && arr[j] < arr[j - gap];j -= gap){
                swap(arr,j,j - gap);
            }
        }
    }
    private void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        res++;
    }
    //判断是否有序
    private boolean isOk(int[] arr){
        for(int i = 1;i < arr.length;i++){
            if(arr[i] >= arr[i - 1]){

            }else{
                return false;
            }
        }
        return true;
    }
}
